package cn.iocoder.yudao.module.crossborder.compliance.controller.admin;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.crossborder.compliance.api.dto.ComplianceCheckDTO;
import cn.iocoder.yudao.module.crossborder.compliance.service.ComplianceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 合规管理
 *
 * @author LHL
 */
@Tag(name = "管理后台 - 合规管理")
@RestController
@RequestMapping("/crossborder/compliance")
@Validated
public class ComplianceController {

    @Resource
    private ComplianceService complianceService;

    @GetMapping("/check/get")
    @Operation(summary = "获得合规检查记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crossborder:compliance:query')")
    public CommonResult<ComplianceCheckDTO> getComplianceCheck(@RequestParam("id") Long id) {
        ComplianceCheckDTO complianceCheck = complianceService.getComplianceCheck(id);
        return success(complianceCheck);
    }

    @GetMapping("/check/list")
    @Operation(summary = "获得合规检查记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('crossborder:compliance:query')")
    public CommonResult<List<ComplianceCheckDTO>> getComplianceCheckList(@RequestParam("ids") List<Long> ids) {
        List<ComplianceCheckDTO> complianceChecks = complianceService.getComplianceCheckList(ids);
        return success(complianceChecks);
    }

    @PostMapping("/check/create")
    @Operation(summary = "创建合规检查记录")
    @PreAuthorize("@ss.hasPermission('crossborder:compliance:create')")
    public CommonResult<Long> createComplianceCheck(@RequestBody @Valid ComplianceCheckDTO complianceCheck) {
        Long id = complianceService.createComplianceCheck(complianceCheck);
        return success(id);
    }

    @GetMapping("/check/validate")
    @Operation(summary = "校验商品合规性")
    @Parameter(name = "productId", description = "商品编号", required = true, example = "1024")
    @Parameter(name = "countryCode", description = "国家代码", required = true, example = "US")
    @PreAuthorize("@ss.hasPermission('crossborder:compliance:validate')")
    public CommonResult<Boolean> validateProductCompliance(@RequestParam("productId") Long productId,
                                                          @RequestParam("countryCode") String countryCode) {
        Boolean isCompliant = complianceService.validateProductCompliance(productId, countryCode);
        return success(isCompliant);
    }

}
